package com.awanp3.nimbus.base.controller;

import java.util.List;

import com.awanp3.nimbus.base.domain.bo.BasePlantBo;
import com.awanp3.nimbus.base.domain.vo.BasePlantVo;
import com.awanp3.nimbus.base.service.IBasePlantService;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.awanp3.nimbus.common.idempotent.annotation.RepeatSubmit;
import com.awanp3.nimbus.common.log.annotation.Log;
import com.awanp3.nimbus.common.web.core.BaseController;
import com.awanp3.nimbus.common.mybatis.core.page.PageQuery;
import com.awanp3.nimbus.common.core.domain.R;
import com.awanp3.nimbus.common.core.validate.AddGroup;
import com.awanp3.nimbus.common.core.validate.EditGroup;
import com.awanp3.nimbus.common.log.enums.BusinessType;
import com.awanp3.nimbus.common.excel.utils.ExcelUtil;
import com.awanp3.nimbus.common.mybatis.core.page.TableDataInfo;

/**
 * 工厂信息
 * 前端访问路由地址为:/base/plant
 *
 * @author 20014170
 * @date 2025-08-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/plant")
public class BasePlantController extends BaseController {

    private final IBasePlantService basePlantService;

    /**
     * 查询工厂信息列表
     */
    @SaCheckPermission("base:plant:page")
    @GetMapping("/page")
    public TableDataInfo<BasePlantVo> page(BasePlantBo bo, PageQuery pageQuery) {
        return basePlantService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询工厂信息列表
     */
    @SaCheckPermission("base:plant:list")
    @GetMapping("/list")
    public R<List<BasePlantVo>> list(BasePlantBo bo) {
        return R.ok(basePlantService.queryList(bo));
    }

    /**
     * 导出工厂信息列表
     */
    @SaCheckPermission("base:plant:export")
    @Log(title = "工厂信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BasePlantBo bo, HttpServletResponse response) {
        List<BasePlantVo> list = basePlantService.queryList(bo);
        ExcelUtil.exportExcel(list, "工厂信息", BasePlantVo.class, response);
    }

    /**
     * 获取工厂信息详细信息
     *
     * @param plantId 主键
     */
    @SaCheckPermission("base:plant:query")
    @GetMapping("/{plantId}")
    public R<BasePlantVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("plantId") Long plantId) {
        return R.ok(basePlantService.queryById(plantId));
    }

    /**
     * 新增工厂信息
     */
    @SaCheckPermission("base:plant:add")
    @Log(title = "工厂信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BasePlantBo bo) {
        return toAjax(basePlantService.insertByBo(bo));
    }

    /**
     * 修改工厂信息
     */
    @SaCheckPermission("base:plant:edit")
    @Log(title = "工厂信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BasePlantBo bo) {
        return toAjax(basePlantService.updateByBo(bo));
    }

    /**
     * 删除工厂信息
     *
     * @param plantIds 主键串
     */
    @SaCheckPermission("base:plant:remove")
    @Log(title = "工厂信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{plantIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("plantIds") Long[] plantIds) {
        return toAjax(basePlantService.deleteWithValidByIds(List.of(plantIds), true));
    }
}
